package com.ainge.easystudy.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author: Ainge
 * @Time: 2021/6/11 01:36
 */
public class CallableDemo {

    public static void main(String[] args) {

        System.out.println(Runtime.getRuntime().availableProcessors());
        System.out.println(Runtime.getRuntime().freeMemory());
        FutureTask<Integer> task = new FutureTask<>(() -> {
            System.out.println(Thread.currentThread().getName() + " come in ...");
            return  1024;
        });
        Thread thread = new Thread(task, "call-name-1");
        Thread thread1 = new Thread(task, "call-name-2");
        thread.start();
        thread1.start();
        try {
            while (!task.isDone()){}
            Integer integer = task.get();
            System.out.println(integer);
        } catch (InterruptedException ignored) {
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

}
